package com.mocoop.activity;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.widget.RelativeLayout;

import com.mocoop.entity.PageInfoEntity;
import com.mocoop.ui.MocoopFragment;

public class MocoopMultitionActivity extends MocoopActivity {

	protected RelativeLayout fragmentsLayout;
	protected List<MocoopFragment> history = new ArrayList<MocoopFragment>();
    private final String FIRST_FRAGMENT_NAME = "firstFragmentName"; 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initView();
		
		currentFragment = new MocoopFragment();
		currentFragment.setCordovaWebView(appView);
		currentFragment.setLeftLayoutEvent(getLeftLayoutEvent());
		getFragmentManager().beginTransaction()
		.replace(fragmentsLayout.getId(), currentFragment)
		.addToBackStack(FIRST_FRAGMENT_NAME).commit();
		history.add(currentFragment);
	}

	public void initView() {
		setContentView(R.layout.activity_multition);
		fragmentsLayout = (RelativeLayout) findViewById(R.id.fragmentsLayout);
	}

	@Override
	public void jumpNewPage(final PageInfoEntity pageInfo) {
		currentFragment.hidePromptView();

		MocoopFragment fragment = new MocoopFragment();
		fragment.setCordovaWebView(createdWebView());
		fragment.setPageInfo(pageInfo);
		fragment.setLeftLayoutEvent(getLeftLayoutEvent());
		String url; 
		if(pageInfo.getUrl().startsWith("http")){
			url = pageInfo.getUrl();
		}else{
			url = getBASE_URL()+pageInfo.getUrl();
		}
		fragment.loadURL(url);
		
		getFragmentManager().beginTransaction()
				.setCustomAnimations(
						R.anim.fragment_slide_left_enter, 
						R.anim.fragment_slide_left_exit, 
						R.anim.fragment_slide_right_enter,
						R.anim.fragment_slide_right_exit)
				.replace(fragmentsLayout.getId(), fragment)
				.addToBackStack(fragment.getLaunchURL()).commitAllowingStateLoss();
		currentFragment = fragment;
		history.add(fragment);
	}

	@Override
	public void goBack(MocoopFragment.MyAnimationCallBack animationCallBack) {
		if (history.size() > 0) {
			executeBack(null);
		}
	}

	private void executeBack(String url) {
		history.remove(currentFragment);
		currentFragment = history.get(history.size() - 1);
		currentFragment.getCordovaWebView().loadUrl("javascript:cordova.fireDocumentEvent('ViewDidAppear');");
		getFragmentManager().popBackStackImmediate(url, 0);
	}

	@Override
	public void showMeRightNow() {
		currentFragment.hideLoadView(null);
		currentFragment.hidePromptView();
	}

	@Override
	public boolean backToSpecifyPage(String targetURL) {
		boolean isExist = false;
		if (history.size() > 2) {
			for (int index = history.size() - 2; index > 0; index--) {
				MocoopFragment fragment = history.get(index);
				if (fragment.getLaunchURL().indexOf(targetURL) >= 0) {
					targetURL = fragment.getLaunchURL();
					isExist = true;
					break;
				} else {
					history.remove(fragment);
				}
			}
			
			if (!isExist) {
				targetURL = FIRST_FRAGMENT_NAME;
			}
		}else{
			targetURL = null;
		}
		
		executeBack(targetURL);
		return isExist;
	}

	

	@Override
	public void onButtonBack() {

	}

	@Override
	public void onButtonMenu() {

	}

	@Override
	public void notificationToUI(String js, String noticePages) {

	}

	@Override
	public List<MocoopFragment> getHistoryList() {
		return history;
	}


}
